package com.qys.livesMall.navy.service;

import com.qys.livesMall.navy.dao.NavyTaskDao;
import com.qys.livesMall.navy.dao.NavyTaskDetailDao;
import com.qys.livesMall.navy.model.entity.NavyTask;
import com.qys.livesMall.navy.model.entity.NavyTaskDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @menu NavyTaskTX
 * @date 2023/07/18 10:33
 */
@Component
public class NavyTaskTX {
    @Autowired
    private NavyTaskDao navyTaskDao;

    @Autowired
    private NavyTaskDetailDao detailDao;

    @Transactional(rollbackFor = Exception.class)
    public void navyPlanOrder(NavyTask task, List<NavyTaskDetail> details){
        if (task==null){
            return;
        }
        navyTaskDao.save(task);
        details.stream().forEach(detail -> {
            detail.setTaskId(task.getId());
        });
        if (CollectionUtils.isEmpty(details)){
            return;
        }
        detailDao.saveBatch(details);
    }

}

